//
//  StatusCell.swift
//  Weibo
//
//  Created by 明丽 on 16/1/18.
//  Copyright © 2016年 明丽. All rights reserved.
//

import UIKit
import SnapKit
class StatusCell: UITableViewCell {
    //定义底部视图的顶部约束的属性
    var bottomViewTopConstraints: Constraint?
    
    var status: Status? {
        didSet{
            statusOriginalView.status = status
            
            //先卸载约束
            self.bottomViewTopConstraints?.uninstall()
            if let re = status?.retweeted_status {
                //1.设置数据源
                //给转发微博设置模型数据
                statusCellRetweetedView.retweeted_status = re
                //2显示转发微信视图
                statusCellRetweetedView.hidden = false
                
                //3,显示约束
                
                statusCellButtonView.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.bottomViewTopConstraints = make.top.equalTo(statusCellRetweetedView.snp_bottom).constraint
                })
                
            }else{
                //1.隐藏转发微博视图
                statusCellRetweetedView.hidden = true
                statusCellButtonView.snp_updateConstraints(closure: { (make) -> Void in
                    
                    
                    self.bottomViewTopConstraints = make.top.equalTo(statusOriginalView.snp_bottom).constraint
                    
                })
            }
            
            
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        //设置选中颜色
        selectionStyle = .None
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置ui界面
    private func setupUI() {
       // backgroundColor = UIColor.purpleColor()
        //添加子视图
        contentView.addSubview(statusOriginalView)
        contentView.addSubview(statusCellRetweetedView)
        contentView.addSubview(statusCellButtonView)
        //设置约束
        statusOriginalView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(contentView)
            //设置 测试 高度
            //make.height.equalTo(80)
        }
        statusCellRetweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(statusOriginalView.snp_bottom)
            
            make.left.equalTo(statusOriginalView.snp_left)
            make.right.equalTo(statusOriginalView.snp_right)
        }
        
        
        
        
        //设置button的约束
        
        statusCellButtonView.snp_makeConstraints { (make) -> Void in
            self.bottomViewTopConstraints = make.top.equalTo(statusCellRetweetedView.snp_bottom).constraint
            make.left.right.equalTo(contentView)
            make.height.equalTo(35)
        }
        
        
        //设置contenView的约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.left.right.top.equalTo(self)
            make.bottom.equalTo(statusCellButtonView.snp_bottom)
            
        }
        
        
        
    }
    //原创微博
    private lazy var statusOriginalView: StatusOriginalView = StatusOriginalView()
    private lazy var statusCellButtonView: statusCellOriginalView = statusCellOriginalView()
    private lazy var statusCellRetweetedView: StatusCellRetweetedView = StatusCellRetweetedView()
    
}
